#include "GameController.h"
#include "../OpenGL.h"
#include "Logger.h"
#include "../OpenGL.h"
#include "GameLib.h"

GameController gameController;

GameController::GameController(){
    title  = DEFAULT_WINDOW_NAME;
    width  = DEFAULT_WINDOW_WIDTH;
    height = DEFAULT_WINDOW_HEIGHT;
	fps = DEFAULT_FPS;baseTime = 0;baseTimeFull = 0;fpsReadout=0;
}
GameController::~GameController(){}
Str&			GameController::getTitle(){
    return title;
}
int				GameController::getWidth(){
    return width;
}
GameController&	GameController::setWidth(int width){
    //NEEDS TO BE DONE
    return *this;
}
GameController&	GameController::sysSetWidth(int width){
    this->width=width;
    return *this;
}
int				GameController::getHeight(){
    return height;
}
GameController&	GameController::setHeight(int height){
    //NEEDS TO BE DONE
    return *this;
}
GameController&	GameController::sysSetHeight(int width){
    this->height=height;
    return *this;
}
double			GameController::getWindowRatio(){
	return width/(double)height;
}
void			GameController::init(){
	glFrontFace(GL_CW);
#ifdef DO_NOT_DRAW_UNDER
	glEnable(GL_CULL_FACE);
#endif
	glEnable(GL_DEPTH_TEST);
	glDepthMask(true);
	glShadeModel(GL_FLAT);
	initFog();
    initLighting();
}
void			GameController::initFog(){
#if ENABLE_FOG == true
	enableFog();
	setFogColor(DEFAULT_FOG_COLOR);
	//setFogDensity(0.01);
	setFogRange(DEFAULT_FOG_RANGE_START, DEFAULT_FOG_RANGE_END);
	setFogModeLinear();
#endif
}
void			GameController::initLighting(){
    if (ENABLE_LIGHTING)
    {
        mainLog.print("Lighting enabled!");
        glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE);
        glEnable(GL_LIGHTING);
        glShadeModel(GL_SMOOTH);
        glEnable(GL_COLOR_MATERIAL);
    }
}
bool			GameController::isLightingEnabled(){
    return ENABLE_LIGHTING;
}
int				GameController::getFPS(){return fps;}
GameController&	GameController::setFPS(int fps){if (fps>0){this->fps=fps;}return *this;}
void			GameController::__sysFPS(){
	calcFPS();
}
void			GameController::calcFPS(){
	frame++;
	int time;
	time=glutGet(GLUT_ELAPSED_TIME);
	if (time - baseTimeFull > 1000)
	{
		/*mainLog.print(Str()<<"FPS: "<<
					  (int)(frame*1000.0/(time-baseTimeFull)));*/
		baseTimeFull = time;		
		frame = 0;
	}
}

//FOG//
GameController&	GameController::enableFog(){
	glEnable(GL_FOG);
	return *this;
}
GameController& GameController::disableFog(){
	glDisable(GL_FOG);
	return *this;
}
bool			GameController::isFogEnabled(){
	GLboolean isEnabled;
	glGetBooleanv(GL_FOG, &isEnabled);
	return isEnabled;
}
GameController&	GameController::setFogColor(int color){
	float cc[3];
	cc[0]=(color&0xFF0000)/(float)0xFF0000;
	cc[1]=(color&0x00FF00)/(float)0x00FF00;
	cc[2]=(color&0x0000FF)/(float)0x0000FF;
	glFogfv(GL_FOG_COLOR, cc);
	return *this;
}
GameController&	GameController::setFogRange(double start, double end){
	glFogf(GL_FOG_START, start);
	glFogf(GL_FOG_END, end);
	return *this;
}
GameController&	GameController::setFogModeLinear(){
	glFogi(GL_FOG_MODE, GL_LINEAR);
	return *this;
}
GameController&	GameController::setFogModeExp(){
	glFogi(GL_FOG_MODE, GL_EXP);
	return *this;
}
GameController&	GameController::setFogModeExp2(){
	glFogi(GL_FOG_MODE, GL_EXP2);
	return *this;
}
GameController&	GameController::setFogDensity(double density){
	glFogf(GL_FOG_DENSITY, density);
	return *this;
}
//END FOG//